@using Extension.k8s
@using k8s.Models
@using Extension
@using BlazorApp.Service.k8s
@using BlazorApp.Utils
@inherits BlazorApp.Pages.Common.PageBase

@if (Items != null)
{
    <Divider Orientation="left" Style="font-weight:bold">Jobs</Divider>
    <Table TItem="V1Job" DataSource="@Items"
           HidePagination="true" PageSize="5000"
           Size=@TableSize.Small Bordered="true">
        <AntDesign.Column Title=@L["Name"] TData="string">
            <Button OnClick="@(() => OnJobClick(context))" Type="@ButtonType.Link">@context.Name()</Button>
        </AntDesign.Column>
        <AntDesign.Column Sortable="true" TData="string" Title=@L["Namespace"] Field="@context.Namespace()"/>
        <AntDesign.Column Title=@L["Pods"] TData="int">
            @CountPodsByOwner(@context.Metadata.Uid)
        </AntDesign.Column>
        <AntDesign.Column Title=@L["Age"] TData="string" Width="100px">
            @context.CreationTimestamp()?.AgeFromUtc()
        </AntDesign.Column>

    </Table>
}

@code {


    [Inject]
    private IJobService JobService { get; set; }

    [Inject]
    private IPodService PodService { get; set; }

    [Inject]
    private DrawerService DrawerService { get; set; }

    private IList<V1Pod> PodList { get; set; }
    public  IList<V1Job> Items   { get; set; }

    [Parameter]
    public string ControllerByUid { get; set; }

    protected override async Task OnInitializedAsync()
    {
        if (!string.IsNullOrEmpty(ControllerByUid))
        {
            Items = JobService.ListByOwnerUid(ControllerByUid);
        }

        PodList = PodService.List();
        await base.OnInitializedAsync();
    }


    private async Task OnJobClick(V1Job job)
    {
        await PageDrawerHelper<V1Job>.Instance
            .SetDrawerService(DrawerService)
            .ShowDrawerAsync<JobDetailView, V1Job, bool>(job);
    }

    private int CountPodsByOwner(string uid)
    {
        return PodList.CountPodsByOwner(uid);
    }

}
